package com.qf.sy2103.fmmall.service.impl;

import com.qf.sy2103.common.vo.ResStatus;
import com.qf.sy2103.common.vo.ResultVO;
import com.qf.sy2103.fmmall.beans.entity.ShoppingCart;
import com.qf.sy2103.fmmall.beans.vo.ShoppingCartVo;
import com.qf.sy2103.fmmall.mapper.ShoppingCartMapper;
import com.qf.sy2103.fmmall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO addShopCart(ShoppingCart shoppingCart) {
        final ResultVO resultVO = new ResultVO();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String dateStr = simpleDateFormat.format(new Date());
            shoppingCart.setCartTime(dateStr);
            final int i = shoppingCartMapper.insert(shoppingCart);
            if (i == 1) {
                resultVO.setCode(ResStatus.OK);
                resultVO.setMsg("success");
            } else {
                resultVO.setCode(ResStatus.NO);
                resultVO.setMsg("数据插入失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listByUserId(Integer userId) {
        final ResultVO resultVO = new ResultVO();
        try {
            List<ShoppingCartVo> vos = shoppingCartMapper.listByUserId(userId);

            resultVO.setData(vos);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");

        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO update(Integer cid, Integer cnum) {

        final ResultVO resultVO = new ResultVO();

        final ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setCartId(cid);
        shoppingCart.setCartNum(cnum+"");
        final int i = shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);

        if (i==1){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
//            resultVO.setData();
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("更新购物车失败");
        }

        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO listbycids(String cids) {

        ResultVO resultVO = new ResultVO();

        final String[] split = cids.split(",");
        final List<String> cidList = Arrays.asList(split);

        List<ShoppingCartVo> vos = shoppingCartMapper.listbycids(cidList);

        if (vos.size() == 0){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有查到购物车信息");
        }else{
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(vos);
        }

        return resultVO;

    }
}
